package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.EntityCustomer;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.EntityDeviceType;
import com.ruoyi.system.service.IEntityDeviceTypeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 器材类型Controller
 * 
 * @author ruoyi
 * @date 2020-12-30
 */
@Controller
@RequestMapping("/system/type")
public class EntityDeviceTypeController extends BaseController
{
    private String prefix = "system/type";

    @Autowired
    private IEntityDeviceTypeService entityDeviceTypeService;

    @RequiresPermissions("system:type:view")
    @GetMapping()
    public String type()
    {
        return prefix + "/type";
    }

    /**
     * 查询器材类型列表
     */
    @RequiresPermissions("system:type:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EntityDeviceType entityDeviceType)
    {
        startPage();
        List<EntityDeviceType> list = entityDeviceTypeService.selectEntityDeviceTypeList(entityDeviceType);
        return getDataTable(list);
    }

    /**
     * 导出器材类型列表
     */
    @RequiresPermissions("system:type:export")
    @Log(title = "导出器材类型列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EntityDeviceType entityDeviceType)
    {
        List<EntityDeviceType> list = entityDeviceTypeService.selectEntityDeviceTypeList(entityDeviceType);
        ExcelUtil<EntityDeviceType> util = new ExcelUtil<EntityDeviceType>(EntityDeviceType.class);
        return util.exportExcel(list, "type");
    }

    /**
     * 新增器材类型
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存器材类型
     */
    @RequiresPermissions("system:type:add")
    @Log(title = "新增保存器材类型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult addSave(EntityDeviceType entityDeviceType)
    {
        return toAjax(entityDeviceTypeService.insertEntityDeviceType(entityDeviceType));
    }

    /**
     * 修改器材类型
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        EntityDeviceType entityDeviceType = entityDeviceTypeService.selectEntityDeviceTypeById(id);
        mmap.put("entityDeviceType", entityDeviceType);
        return prefix + "/edit";
    }

    /**
     * 修改保存器材类型
     */
    @RequiresPermissions("system:type:edit")
    @Log(title = "修改保存器材类型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult editSave(EntityDeviceType entityDeviceType)
    {
        return toAjax(entityDeviceTypeService.updateEntityDeviceType(entityDeviceType));
    }

    /**
     * 删除器材类型
     */
    @RequiresPermissions("system:type:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(entityDeviceTypeService.deleteEntityDeviceTypeByIds(ids));
    }


    /**
     * 检查是否已经有该名称
     */
    @PostMapping("/checkName")
    @ResponseBody
    public boolean checkName(EntityDeviceType entityDeviceType){
        String name = entityDeviceType.getName();
        if(StringUtils.isEmpty(name)){
            return false;
        }
        boolean existName = entityDeviceTypeService.isCheckDeviceTypeName(name,entityDeviceType.getId());
        return !existName;
    }

    /**
     * 加载部门列表树
     */
    @GetMapping("/getValidDeviceTypeTree")
    @ResponseBody
    public List<Ztree> getValidDeviceTypeTree()
    {
        List<Ztree> ztrees = entityDeviceTypeService.getValidDeviceTypeTree();
        Ztree ztree = new Ztree();
        ztree.setId(0L);
        ztree.setName("全部");
        ztree.setTitle("全部");
        ztrees.add(ztree);
        return ztrees;
    }

    @GetMapping(value = { "/gotoDeptTree"})
    public String selectDeptTree() {
        return prefix + "/tree";
    }
}
